package com.xiaotu.service;

import com.xiaotu.dao.*;
import com.xiaotu.model.*;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @作者 王艳龙
 * @日期 2021-03-10 17:12
 * @功能
 */
@Service
@Transactional
public class RentApplyService {

    @Autowired
    private RentApplyDao rentApplyDao;
    @Autowired
    private CultureRelatedDao cultureDao;
    @Autowired
    private CultureAllRelatedDao cultureAllDao;
    @Autowired
    private RentShareholderDao shareholderDao;
    @Autowired
    private RentHonorDao honorDao;
    @Autowired
    private RentAccountDao accountDao;
    @Autowired
    private RentContactsDao contactsDao;
    @Autowired
    private RentInfoDao rentInfoDao;
    @Autowired
    private CommonApplyDao commonApplyDao;
    @Autowired
    private CommonApplyRecordDao commonApplyRecordDao;

    /**
     * 获取企业房租通申请列表
     * @param rentModel
     * @return
     */
    public List<TabEnterpriseRentApplyInfo> queryRentApplyInfo(TabEnterpriseRentApplyInfo rentModel){
        return rentApplyDao.queryRentApplyInfo(rentModel);
    }

    /**
     * 获取企业房租通基本信息列表
     * @param applyId
     * @return
     */
    public TabEnterpriseRentInfo queryRentInfo(Integer applyId){
        return rentInfoDao.queryRentInfo(applyId);
    }

    /**
     * 后台获取房租通申请记录
     * @param authId
     * @param approverId
     * @param enterpriseName
     * @param declareYear
     * @param applyStatusDesc
     * @return
     */
    public List<Map<String,Object>> queryRentApplyByAuthId(String authId,String approverId,
            String enterpriseName,String declareYear,String applyStatusDesc){
        return rentApplyDao.queryRentApplyByAuthId(authId,approverId,enterpriseName,declareYear,applyStatusDesc);
    }

    /**
     * 获取所属园区名称
     * @param enterpriseParkId
     * @return
     */
    public List<Map<String,Object>> queryEnterpriseParkName(@Param("enterpriseParkId") String enterpriseParkId){
        return rentInfoDao.queryEnterpriseParkName(enterpriseParkId);
    }

    /**
     * 新增修改房租通申请
     * @param rentApplyModel
     * @param rentInfoModel
     */
    public Map<String,Object> addOrupdateRentInfo(TabEnterpriseRentApplyInfo rentApplyModel,TabEnterpriseRentInfo rentInfoModel) {
        Map<String,Object> map = new HashMap<String,Object>();
        //新增修改房租通申请记录
        if(null==rentApplyModel.getId()){
            TabCommonApply commonModel = new TabCommonApply();
            if(null == rentApplyModel.getCommentApplyId()){
                //新增状态记录表
                commonModel.setAuthId(rentApplyModel.getServerAuthId());
                commonModel.setApplyStatusDesc("暂存");
                commonApplyDao.insertSelective(commonModel);
                rentApplyModel.setCommentApplyId(commonModel.getApplyId());
            }
            rentApplyModel.setCreateTime(new Date());
            rentApplyDao.insertSelective(rentApplyModel);
        }else{
            rentApplyDao.updateByPrimaryKeySelective(rentApplyModel);
        }
        //新增修改房租通基本信息
        if(null != rentApplyModel.getId()){
            if(null==rentInfoModel.getBasicId()){
                rentInfoModel.setCreateTime(new Date());
                rentInfoModel.setApplyId(rentApplyModel.getId());
                rentInfoDao.insertSelective(rentInfoModel);
            }else{
                rentInfoDao.updateByPrimaryKeySelective(rentInfoModel);
            }
        }

            map.put("applyId",rentApplyModel.getId());
            map.put("basicId",rentInfoModel.getBasicId());
            return map;
    }

    /**
     * 根据id删除房租通申请
     * @param id
     */
    public void delRentApplyInfo(Integer id) {
        //删除房租通申请
        rentApplyDao.deleteByPrimaryKey(id);
        //删除房租通基本信息
        delRentInfo(id);
        //删除房租通股东企业记录
        delRentShareholderInfo(id);
        //删除房租通所获荣誉记录
        delRentHonorInfo(id);
        //删除房租通账户记录
        delRentAccountInfo(id);
        //删除房租通企业联系人记录
        delRentContactsInfo(id);
        //删除房租通审批状态记录
        delRentCommonApplyInfo(id);
        //删除房租通审批节点记录
        delRentCommonApplyRecordInfo(id);
    }

    /**
     * 根据applyId删除房租通基本信息
     * @param applyId
     */
    public void delRentInfo(Integer applyId) {
        Example example = new Example(TabEnterpriseRentInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId",applyId);
        rentInfoDao.deleteByExample(example);
    }

    /**
     * 根据applyId删除房租通股东企业信息
     * @param applyId
     */
    public void delRentShareholderInfo(Integer applyId) {
        Example example = new Example(TabEnterpriseRentShareholderInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId",applyId);
        shareholderDao.deleteByExample(example);
    }

    /**
     * 根据applyId删除房租通所获荣誉记录
     * @param applyId
     */
    public void delRentHonorInfo(Integer applyId) {
        Example example = new Example(TabEnterpriseRentHonorInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId",applyId);
        honorDao.deleteByExample(example);
    }

    /**
     * 根据applyId删除房租通账户信息
     * @param applyId
     */
    public void delRentAccountInfo(Integer applyId) {
        Example example = new Example(TabEnterpriseRentAccountInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId",applyId);
        accountDao.deleteByExample(example);
    }

    /**
     * 根据applyId删除房租通联系人记录
     * @param applyId
     */
    public void delRentContactsInfo(Integer applyId) {
        Example example = new Example(TabEnterpriseRentContactsInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId",applyId);
        contactsDao.deleteByExample(example);
    }

    /**
     * 根据applyId删除房租通审批状态记录
     * @param applyId
     */
    public void delRentCommonApplyInfo(Integer applyId) {
        Example example = new Example(TabCommonApply.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId",applyId);
        commonApplyDao.deleteByExample(example);
    }

    /**
     * 根据applyId删除房租通审批节点记录
     * @param applyId
     */
    public void delRentCommonApplyRecordInfo(Integer applyId) {
        Example example = new Example(TabCommonApplyRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId",applyId);
        commonApplyRecordDao.deleteByExample(example);
    }

    /**
     * 根据parentId获取大中小分类名称
     * @param model
     * @return
     */
    public List<TabCultureRelatedInfo> queryCodeNameByParentId(TabCultureRelatedInfo model){
        Example example = new Example(TabCultureRelatedInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isEmpty(model.getCodeNum())){
            criteria.andEqualTo("parentId",0);
        }else{
            criteria.andEqualTo("parentId",model.getCodeNum());
        }
        return  cultureDao.selectByExample(example);
    }

    /**
     * 根据分类代码获取分类名称
     * @param parentId
     * @return
     */
    public TabCultureRelatedInfo querySpliceCodeName(String parentId){
        Example example = new Example(TabCultureRelatedInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("codeNum",parentId);
        return cultureDao.selectOneByExample(example);
    }

    /**
     * 根据部分分类编码获取记录
     * @param classCode
     * @return
     */
    public TabCultureRelatedInfo queryVerifyClassCode(String classCode){
        Example example = new Example(TabCultureRelatedInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("guildClassCode",classCode);
        return cultureDao.selectOneByExample(example);
    }

    /**
     * 根据所有分类编码获取记录
     * @param classCode
     * @return
     */
    public TabCultureAllRelatedInfo queryVerifyAllClassCode(String classCode){
        Example example = new Example(TabCultureAllRelatedInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("guildClassCode",classCode);
        return cultureAllDao.selectOneByExample(example);
    }

    /**
     * 拼接文化产业分类名称
     * @param model
     * @param list
     * @return
     */
    public StringBuffer getAllCodeName(TabCultureRelatedInfo model,List<String> list){
        if(!model.getParentId().equals("0")){
            if(null !=model.getParentId()){
                model =  querySpliceCodeName(model.getParentId());
                if(null !=model && null !=model.getCodeName()){
                    list.add(model.getCodeName());
                    getAllCodeName(model,list);
                }

            }
        }
        StringBuffer sub = new StringBuffer();
        Collections.reverse(list);
        for (String str : list) {
            sub.append(str).append("-");
        }
        sub.deleteCharAt(sub.length() - 1);

        return sub;
    }

    /**
     * 获取企业房租通股东信息和关联企业记录
     * @param applyId
     * @return
     */
    public List<TabEnterpriseRentShareholderInfo> queryShareholderInfo(String applyId,Integer status) {
        Example example = new Example(TabEnterpriseRentShareholderInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId",applyId);
        criteria.andEqualTo("status",status);
        example.setOrderByClause("create_time desc");
        return shareholderDao.selectByExample(example);
    }

    /**
     * 批量新增修改企业股东信息
     * @param shareholderInfo
     */
    public void addOrUpdateShareholderInfo(TabEnterpriseRentShareholderInfo shareholderInfo) {
        if(null ==shareholderInfo.getShareholderId()){
            shareholderInfo.setCreateTime(new Date());
            shareholderDao.insertSelective(shareholderInfo);
        }else{
            shareholderDao.updateByPrimaryKeySelective(shareholderInfo);
        }

    }

    /**
     * 新增修改企业所获荣誉信息
     * @param honorInfo
     */
    public void addOrUpdateHonorInfo(TabEnterpriseRentHonorInfo honorInfo) {
        if(null ==honorInfo.getHonorId()){
            honorInfo.setCreateTime(new Date());
            honorDao.insertSelective(honorInfo);
        }else{
            honorDao.updateByPrimaryKeySelective(honorInfo);
        }

    }

    /**
     * 新增修改企业账户信息
     * @param accountInfo
     */
    public void addOrUpdateAccountInfo(TabEnterpriseRentAccountInfo accountInfo) {
        //新增修改账户信息
        if(null ==accountInfo.getAccountId()){
            accountInfo.setCreateTime(new Date());
            accountDao.insertSelective(accountInfo);
        }else{
            accountDao.updateByPrimaryKeySelective(accountInfo);
        }

    }

    /**
     * 删除企业房租通股东信息和关联企业记录
     * @param shareholderId
     */
    public void delShareholderInfo(Integer shareholderId) {
        shareholderDao.deleteByPrimaryKey(shareholderId);
    }

    /**
     * 获取所获荣誉记录
     * @param applyId
     * @return
     */
    public List<TabEnterpriseRentHonorInfo> queryHonorInfo(String applyId) {
        Example example = new Example(TabEnterpriseRentHonorInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId",applyId);
        example.setOrderByClause("create_time desc");
        return honorDao.selectByExample(example);
    }

    /**
     * 删除所获荣誉记录
     * @param honorId
     */
    public void delHonorInfo(Integer honorId) {
        honorDao.deleteByPrimaryKey(honorId);
    }

    /**
     * 删除账户记录
     * @param accountId
     */
    public void delAccountInfo(Integer accountId) {
        accountDao.deleteByPrimaryKey(accountId);
    }

    /**
     * 获取账户信息记录
     * @param applyId
     * @return
     */
    public List<TabEnterpriseRentAccountInfo> queryAccountInfo(String applyId) {
        Example example = new Example(TabEnterpriseRentAccountInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId",applyId);
        example.setOrderByClause("create_time desc");
        return accountDao.selectByExample(example);
    }

    /**
     * 获取企业联系人记录
     * @param applyId
     * @return
     */
    public TabEnterpriseRentContactsInfo queryContactsInfo(Integer applyId) {
        Example example = new Example(TabEnterpriseRentContactsInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId",applyId);
        return contactsDao.selectOneByExample(example);
    }

    /**
     * 修改企业联系人记录
     * @param model
     */
    public void updateContactsInfo(TabEnterpriseRentContactsInfo model) {
        contactsDao.updateByPrimaryKeySelective(model);
    }

    /**
     * 新增企业联系人记录
     * @param model
     */
    public void addContactsInfo(TabEnterpriseRentContactsInfo model) {
        contactsDao.insertSelective(model);
    }


    /**
     * 审批流程完成后，同步企业基本信息
     * @param enterpriseId
     * @param applyId
     */
    public void autosynEnterpriseInfo(String enterpriseId,String applyId){
        //根据申请获取房租通基本信息
        TabEnterpriseRentInfoDto enterpriseUserInfo = rentInfoDao.queryRentInfoByApplId(applyId);

        if(null !=enterpriseUserInfo) {
            //修改企业用户信息
            rentInfoDao.updateEnterpriseUserInfo(enterpriseUserInfo, enterpriseId);
            //enterpriseType 1：文化企业，2：股权投资机构，3：金融机构，4：服务机构，5：园区
            Map<String,Object> levelMap = rentInfoDao.queryEnterpriseLevelId(enterpriseId);
            String enterpriseType = levelMap.get("enterpriseType")+"";
            if (enterpriseType.equals("1")) {
                //修改企业文化信息
                rentInfoDao.updateEnterpriseWhInfo(enterpriseUserInfo, enterpriseId);
            }else if(enterpriseType.equals("2")){
                //修改企业股权投资机构信息
                rentInfoDao.updateEnterpriseXInfo(enterpriseUserInfo, enterpriseId, "tab_enterprise_gq_info");
            }else if(enterpriseType.equals("3")){
                //修改企业金融机构信息
                rentInfoDao.updateEnterpriseXInfo(enterpriseUserInfo, enterpriseId, "tab_enterprise_jr_info");
            }else if(enterpriseType.equals("4")){
                //修改企业服务机构信息
                rentInfoDao.updateEnterpriseXInfo(enterpriseUserInfo, enterpriseId, "tab_enterprise_fw_info");
            }else if(enterpriseType.equals("5")){
                //修改企业园区信息
                rentInfoDao.updateEnterpriseYqInfo(enterpriseUserInfo,enterpriseId);
            }
        }

    }
}
